package com.zb.test;

import java.io.IOException;

import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

public class RoutingKeyCons {
	private static final String QUEUE_QQ = "QQ";

	private static final String QUEUE_WEIXIN = "WEIXIN";

	private static final String EXCHANGE_ROUTING = "EXCHANGE_ROUTING";

	private static final String RUOTING_KEY = "RUOTING_KEY";

	private static final ConnectionFactory factory = new ConnectionFactory();
	
	private DefaultConsumer defaultConsumer = null;

	private Connection connection = null;

	private Channel channel = null;

	public RoutingKeyCons() {
		factory.setHost("106.12.222.85");
		factory.setPort(5672);
		factory.setUsername("guest");
		factory.setPassword("guest");
		factory.setVirtualHost("/");
	}
	
	public void init() {

		try {
			connection = factory.newConnection();
			channel = connection.createChannel();	
			 defaultConsumer = new DefaultConsumer(channel) {

				@Override
				public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties,
						byte[] body) throws IOException {
					
					String message = new String(body,"utf-8");
					System.out.println("recevie:"+message );
				}
			};
			
		} catch (Exception e) {

			e.printStackTrace();
		}
	}
	
	
	public void recevie() {
		try {
		//	channel.queueBind(QUEUE_QQ, EXCHANGE_ROUTING, RUOTING_KEY);
			channel.basicConsume(QUEUE_WEIXIN, true, defaultConsumer);
		} catch (IOException e) {
			
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		RoutingKeyCons  cons = new RoutingKeyCons();
		cons.init();
		cons.recevie();
	}
}
